package com.slz3.business.module.systemManagement.sysFunction.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.systemManagement.sysFunction.domain.SysFunction;
import com.slz3.business.module.systemManagement.sysFunction.domain.SysFunctionFactory;
import com.slz3.business.module.systemManagement.sysFunction.steward.SysFunctionSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class SysFunctionServiceImpl implements SysFunctionService {

    @Override
    public SysFunction save(SysFunction sysFunction) {
        return Repositorys.sysFunction.save(SysFunctionFactory.initBaseFields(sysFunction));
    }

    @Override
    public Page<SysFunction> page(MultiValueMap<String, String> params) {
        Page<SysFunction> sysFunctionPage = Repositorys.sysFunction.findAll(
                SysFunctionSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return sysFunctionPage;
    }

    @Override
    public SysFunction modify(SysFunction sysFunctionNew) {
        return Repositorys.sysFunction.saveAndFlush(SysFunctionFactory.initBaseFields(sysFunctionNew));
    }

    @Override
    public Optional<SysFunction> single(UUID uuid) {
        return Repositorys.sysFunction.findById(uuid);
    }

    @Override
    public List<SysFunction> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.sysFunction.findAllById(uuids);
    }

    @Override
    public List<SysFunction> delete(List<SysFunction> sysFunctionList) {
        sysFunctionList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            SysFunctionFactory.initBaseFields(item);
        });
        return Services.sysFunctionTransactional.batchModify(sysFunctionList);
    }

}
